package net.arvin.mobilebanksystem.fragment;

import java.util.ArrayList;
import java.util.List;

import net.arvin.dao.BindCardsAdapter;
import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.MainActivity;
import net.arvin.model.Card;
import net.arvin.service.CardService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import net.arvin.view.NormalDialog;
import net.arvin.view.NormalDialog.MyDialogListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

public class MyCardFragment extends Fragment implements OnItemClickListener,
		OnItemLongClickListener {
	private ListView cards;
	private List<Card> cardsInfo;
	private BindCardsAdapter mAdapter;
	private ResponseHandler mResponseHandler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mycard, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
	}

	private void initListView() {
		MainActivity.mainActivity.setTitleTv("我的银行卡");
		mAdapter = new BindCardsAdapter(cardsInfo, getActivity());
		cards.setAdapter(mAdapter);
		cards.setOnItemClickListener(this);
		cards.setOnItemLongClickListener(this);
	}

	private void initView(View view) {
		cards = (ListView) view.findViewById(R.id.cards);
		String data = PreferenceUtils.getPreferCards(getActivity());
		if (data == null || data.equals("")) {
			cardsInfo = new ArrayList<Card>();
		} else {
			cardsInfo = JSON.parseArray(data, Card.class);
		}
		initListView();
	}

	private NormalDialog unbindDialog;

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			final int position, long id) {
		unbindDialog = new NormalDialog(getActivity(), R.style.MyDialog,
				new MyDialogListener() {
					@Override
					public void onClick(View view) {
						switch (view.getId()) {
						case R.id.dialog_btn_ensure:
							unbindDialog.dismiss();
							if (NetConnectionUtil.isConnectWithTip(
									getActivity(), "没有联网=_=!")) {
								CardService
										.tryUnbindCard(cardsInfo.get(position)
												.getCardnumber(),
												getResponseHandler(position),
												getActivity());
							}
							break;
						case R.id.dialog_btn_cancel:
							unbindDialog.dismiss();
							break;
						default:
							break;
						}
					}
				});
		unbindDialog.messages = (TextView) unbindDialog.getWindow()
				.findViewById(R.id.dialog_messages);
		unbindDialog.messages.setText("确定要解除绑定吗？");
		unbindDialog.show();
		return false;
	}

	private ResponseHandler getResponseHandler(final int position) {
		if (mResponseHandler == null) {
			mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					Toast.makeText(getActivity(), "解绑成功!", Toast.LENGTH_SHORT)
							.show();
					cardsInfo.remove(position);
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.Cards,
							JSON.toJSONString(cardsInfo), getActivity());
					mAdapter.notifyDataSetChanged();
				}

				@Override
				public void onRequestFailed(final Object data) {
					Toast.makeText(getActivity(), "系统错误=_=!",
							Toast.LENGTH_SHORT).show();
				}
			};
		}
		return mResponseHandler;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Toast.makeText(getActivity(), "卡" + (position + 1) + "被点击~~",
				Toast.LENGTH_SHORT).show();

	}

}
